﻿using Models;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommTestNFW.ImageCode
{
    public class DataHelper
    {
        /// <summary>
        /// 拼接LabNo
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string Join(PrintLabNo model)
        {
            var week = model.Week == 6 ? "6" : "";

            var fullYear = (20 * 100) + model.Year;// 2000 + 20 = 2020年
            var date = new DateTime(fullYear, model.Month, 1);
            var monthE = date.ToString("MMMM", new CultureInfo("en-us")).Substring(0, 1);

            //ZM20P07J60001
            //ZM20P07J0001
            model.LabNo = model.Custom + model.Year + model.SpecimenType + model.Month.ToString("D2") + monthE + week + model.Counter.ToString("D4");
            return model.LabNo;
        }

        public static List<List<PrintLabNo>> GetDoublePages(PrintLabNo[] codes)
        {
            int rows = LabelConfig.RowsCount;
            int cols = LabelConfig.ColsCount;

            //向下转为偶数
            var realRows = rows & (int.MaxValue - 1);
            //双排一份
            List<PrintLabNo> doubleRowPage = GetDouble(codes);
            //双排单页标签数量
            var pageCount = realRows * cols;
            var pages = new List<List<PrintLabNo>>();
            pages = codes
                .Select((s, index) => new { s, index })
                .GroupBy(g => g.index / pageCount, i => i.s)
                .Select(g => g.Select(s => s).ToList())
                .ToList();
            return pages;
        }
        public static List<List<PrintLabNo>> GetPages(PrintLabNo[] codes)
        {
            int rows = LabelConfig.RowsCount;
            int cols = LabelConfig.ColsCount;
            //单排一份
            List<PrintLabNo> doubleRowPage = codes.ToList();
            //单排单页标签数量
            var pageCount = rows * cols;
            var pages = new List<List<PrintLabNo>>();
            pages = codes
                .Select((s, index) => new { s, index })
                .GroupBy(g => g.index / pageCount, i => i.s)
                .Select(g => g.Select(s => s).ToList())
                .ToList();
            return pages;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="codes"></param>
        public static List<PrintLabNo> GetDouble(PrintLabNo[] codes)
        {
            List<PrintLabNo> doubleRowPage = new List<PrintLabNo>();

            int len = codes.Length;
            int rows = LabelConfig.RowsCount;
            int cols = LabelConfig.ColsCount;
            //双行一份

            //单行双份
            for (int i = 0; i < len;)
            {
                for (int j = 0; j < cols; j++)
                    doubleRowPage.Add(codes[j]);
                for (int j = 0; j < cols; j++)
                    doubleRowPage.Add(codes[j]);
                i += cols;
            }
            return doubleRowPage;
        }

        public static IEnumerable<PrintLabNo> GetPrintLabNos(int len, int repeat = 1)
        {
            if (len == 0)
            {
                yield return null;
            }
            var date = DateTime.Now;
            string template = "ZM20{0}06J{1}{2}";
            var random = new Random();
            for (int i = 1; i <= len; i++)
            {
                int r = random.Next(1, 12);
                string p = r % 2 == 0 ? "P" : "";
                int week = r % 3 == 0 ? 6 : 7;
                var fullYear = date.Year;// 2000 + 20 = 2020年
                var labNo = new PrintLabNo()
                {
                    Counter = i,
                    Custom = "ZM",
                    Year = fullYear - 2000,
                    Month = date.Month,
                    SpecimenType = p,
                    Week = week
                };
                Join(labNo);
                for (int j = 0; j < repeat; j++)
                {
                    yield return labNo;
                }
            }
        }
        public static IEnumerable<string> GetCodes(int len)
        {
            if (len == 0)
            {
                yield return null;
            }
            string template = "ZM20{0}06J{1}{2}";
            for (int i = 1; i <= len; i++)
            {
                string serial = i.ToString("0000");
                string p = i % 2 == 0 ? "P" : "";
                string six = i % 2 == 0 ? "6" : "";
                string code = string.Format(template, p, six, serial);

                yield return code;
                yield return code;
                yield return code;
            }
        }
    }
}
